<?php
require( dirname(__FILE__) . '/../../../wp-load.php' ); 
nocache_headers();

$action = $_GET['action'];
$value = $_GET['value'];

if($action == 'checkemail'){
	if(username_exists($value)){
		echo '1';
		exit();
	}else {
		echo '0';
		exit();
	}
}else if($action == 'login'){
	$login_type = $_GET['login_type'];
	if($login_type == 'sina'){
		$sina_id = $_GET['out_id'];
		$display_name = $_GET['out_name'];
		if(!username_exists('[sina]'.$sina_id)){
			$user_activtion_key = wp_generate_password(24, false);
		
			global $wpdb;
			$wpdb->insert('wp_users', array('user_login'=>'[sina]'.$sina_id ,
								'display_name'=>$display_name, 
								'user_registered' => gmdate('Y-m-d H:i:s'), 
								'user_status' => 0));
			$useid = (int) $wpdb->insert_id;
			
			add_user_meta($useid, 'login_type', 'sina');
			add_user_meta($useid, 'ref_id', $sina_id);
			
			$user = get_userdata($useid);
		}else{
			$user = get_userdatabylogin('[sina]'.$sina_id);
		}
		
		$_SESSION['userdata'] = $user;
		echo json_encode($user);
	}else{
		$username = $_GET['username'];
		$password = stripslashes($_GET['password']);
		
		$user = get_userdatabylogin($username);
		if($user){
		
			if(user_pass_ok($username, $password)){
				if (!isset($_SESSION)) {
					session_start();
				}
				$_SESSION['userdata'] = $user;
				echo json_encode($user);
			}else {
				echo 0;
			}
			}else{
			echo 2;
		}
	}
	exit();
	
}else if('user_login'== $action){
	$username = $_GET['username'];
	$password = stripslashes($_GET['password']);
	
	$user = get_userdatabylogin($username);
	
	if($user){
		
		if(user_pass_ok($username, $password)){
			if (!isset($_SESSION)) {
				session_start();
			}
			$_SESSION['userdata'] = $user;
			echo json_encode($user);;
		}else {
			echo 0;
		}
		exit();
	}else{
		echo 0;
		exit();
	}
	
}

?>